package view.controller;

import common.Group;
import common.Message;
import common.NowUser;
import javafx.event.ActionEvent;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.stage.Stage;
import server.ClientConnServer;

import javax.swing.*;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.concurrent.BrokenBarrierException;

public class GroupapplyController implements Initializable {
    public ImageView grapimg;
    public Label grapname;
    public Label grapid;
    public Label groupid;
    public Button graptrue;
    public Button grapfalse;

    public void ongraptrue(ActionEvent actionEvent) throws BrokenBarrierException, InterruptedException, IOException {
        Group group = new Group();
        Message msg = new Message();
        group.setGroupid(groupid.getText());
        group.setuserid(grapid.getText());
        group.setGroupstate(1);
        msg.setType("gapplytrue");
        msg.setGroup(group);
        NowUser.acceptgroup=false;
        new ClientConnServer().sendinfo(msg);
        NowUser.cyclicBarrier.reset();
        NowUser.cyclicBarrier.await();
        if(NowUser.acceptgroup){
            JOptionPane.showMessageDialog(null, "已同意对方入群申请", "提示", JOptionPane.PLAIN_MESSAGE);
            Stage stage = (Stage)graptrue.getScene().getWindow();
            stage.close();
        }else{
            JOptionPane.showMessageDialog(null, "添加失败", "提示", JOptionPane.ERROR_MESSAGE);
        }
    }

    public void ongrapfalse(ActionEvent actionEvent) throws BrokenBarrierException, InterruptedException, IOException {
        Group group = new Group();
        Message msg = new Message();
        group.setGroupid(groupid.getText());
        group.setuserid(grapid.getText());
        msg.setType("gapplyflase");
        msg.setGroup(group);
        NowUser.refusegroup=false;
        new ClientConnServer().sendinfo(msg);
        NowUser.cyclicBarrier.reset();
        NowUser.cyclicBarrier.await();
        if(NowUser.refusegroup){
            JOptionPane.showMessageDialog(null, "已拒绝对方入群申请", "提示", JOptionPane.PLAIN_MESSAGE);
        }else{
            JOptionPane.showMessageDialog(null, "拒绝对方入群申请失败", "提示", JOptionPane.ERROR_MESSAGE);
        }
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        groupid.setText(NowUser.getGroup().getGroupid());
        grapid.setText(NowUser.getGroup().getuserid());
        grapname.setText(NowUser.getGroup().getGroupname());//显示进入群聊的人的id
        if(NowUser.getGroup().getGroupheadimage()!=null && !NowUser.getGroup().getGroupheadimage().equals("") && !NowUser.getGroup().getGroupheadimage().equals("null")){
            Image image=new Image("file:///" + NowUser.getGroup().getGroupheadimage());
            grapimg.setImage(image);
        }else{
            Image image=new Image("view/photo/群头像.jpg");
            grapimg.setImage(image);
        }
    }
}
